// hyb_ylmz/personal_page/detail/patient_detail/patient_detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    img_arr: [],
    data_arr: [],
  },
  radioChange: function(e) {
    //console.log(e)
    var that = this
    var types = e.detail.value
    if (types == '1') {
      that.setData({
        disabled: true
      })
    } else {
      that.setData({
        disabled: false
      })
    }
  },
  chakanimg(e) {
    var that = this;
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: that.data.info.pic,
    })
  },
  //用户选择收货地址
  chooseLocation: function() {
    var that = this;
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function(res) {
          console.log(JSON.stringify(res));

          var address = res.provinceName + res.cityName + res.countyName + res.detailInfo
          console.log(res);
          that.setData({
            address: address,
            //具体收货地址显示
            flag: false,
          })
        },
        fail: function(err) {
          console.log(JSON.stringify(err));
          console.info("收货地址授权失败");

        }
      })
    } else {
      console.log('当前微信版本不支持chooseAddress');
    }
  },
  formSubmit: function(e) {
    console.log(e)
    var that = this
    var values = e.detail.value
    var money = values.nummoney
    console.log(money)
    var content = values.rg //处方内容
    var userid = values.hzid //患者ID
    var docid = values.zid //专家ID
    var username = values.username //患者名字
    var ky_yibao = values.dorder //订单号
    var docname = values.docname
    var data_arr = that.data.data_arr;
    var useropenid = that.data.useropenid
    var address = values.address
    console.log(address, username, docname, ky_yibao)
    var cid = that.data.cid
    var yjfs = values.yjfs
    var openid = wx.getStorageSync('openid')
    if (e.detail.value.rg == "") {
      wx.showToast({
        title: '请填写处方',
        image: "/hyb_yl/images/err.png"
      })
    } else if (yjfs == '1' && address == "") {
      wx.showToast({
        title: '请选择收货地址',
        image: "/hyb_yl/images/err.png"
      })
    } else {
      //支付

      app.util.request({
        'url': 'entry/wxapp/Pay',
        header: {
          "Content-Type": "application/xml"
        },
        method: "GET",
        data: {
          openid: openid,
          z_tw_money: money
        },
        success: function(res) {
          console.log(res)
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr, //字符串随机数
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function(res) {
              //更新订单
              app.util.request({
                'url': 'entry/wxapp/Upcforder',
                data: {
                  cid: cid,
                  address: address,
                  username: username,
                  docname: docname,
                  ky_yibao: ky_yibao,
                  yjfs: yjfs,
                  zid: docid,
                  leixing: '处方',
                  money: money,
                  types: 4
                },
                success: function(res) {
                  console.log(res)
                  //莫板消息通知医生
                  app.util.request({
                    'url': 'entry/wxapp/Tongzhidoc',
                    data: {
                      zid: docid,
                      username: username,
                      ky_yibao: ky_yibao,
                      money: money
                    },
                    success: function(res) {
                      console.log(res)
                    },
                    fail: function(err) {
                      console.log(err)
                    },
                  })
                  that.setData({
                    state: true
                  })
                }
              })
              // app.util.request({
              //   'url': 'entry/wxapp/Saverecipe',
              //   data: {
              //     cid: cid,
              //     address: address,
              //     username: username,
              //     docname: docname,
              //     ky_yibao: ky_yibao
              //   },
              //   success: function(res) {
              //     console.log(res)
              //   },
              //   fail: function(err) {
              //     console.log(err)
              //   },
              // })
            }
          })
        }
      })


    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var username = options.username
    var ksname = options.ksname
    if (options.z_yy_money || options.z_yy_money != undefined) {
      var money = options.z_yy_money
    
    } else {
     
      var money = options.money
    }

    var phone = options.phone
    var tjtime = options.tjtime
    var yytime = options.yytime
    var dorder = options.dorder
    var cfzhenj = options.cfzhenj
    var sex = options.sex
    var age = options.age
    var zjid = options.zjid
    var hzid = options.hzid
    var bgc = wx.getStorageSync('color')
    var cid = options.cid
    var useropenid = options.useropenid
    var uniacid = app.siteInfo.uniacid;
    var state = options.state
    var yjfs = options.yjfs
    if (state == '1') {
      if (yjfs == '0') {
        that.setData({
          state: true,
          disabled: false
        })
      } else {
        that.setData({
          state: true,
          disabled: true
        })
      }
    } else {
      that.setData({
        state: false,
        disabled: false
      })
    }
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    if (options.utype !== 'undefined') {
      that.setData({
        utype: options.utype
      })
    }
    if (cid) {
      app.util.request({
        'url': 'entry/wxapp/Selcetcfinfo',
        data: {
          cid: cid
        },
        success: function(res) {
          console.log(res)
          that.setData({
            dorder: res.data.data.orderarr,
            username: res.data.data.username,
            sex: res.data.data.mysex,
            age: res.data.data.myage,
            phone: res.data.data.myphone,
            dmoney: res.data.data.dmoney,
            money: money,
            nummoney: res.data.data.nummoney,
            content: res.data.data.content,
            info: res.data.data,
            address: res.data.data.address,
            yjfs: res.data.data.yjfs,

          })
        }
      })
    }
    var z_name = options.z_name
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    // 判断当前是否是被确认状态
    var that = this
    var id = options.id
    var ky_yibao = options.ky_yibao
    that.setData({
      username: username,
      ksname: ksname,
      money: money,
      phone: phone,
      tjtime: tjtime,
      yytime: yytime,
      dorder: dorder,
      sex: sex,
      age: age,
      zjid: zjid,
      hzid: hzid,
      z_name: z_name,
      uniacid: uniacid,
      useropenid: useropenid,
      cid: cid,
      cfzhenj: cfzhenj
    })
    //订单号查询用户ID
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getDocinfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */

  getDocinfo: function() {
    var that = this
    var zid = that.data.zjid
    app.util.request({
      'url': 'entry/wxapp/Docinfo',
      data: {
        zid: zid
      },
      success: function(res) {
        console.log(res)
        that.setData({
          z_name: res.data.data.z_name
        })
      }
    })
  }
})